package com.prpat;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.ScrollPaneConstants;

import de.uniba.wiai.lspi.util.logging.SystemOutPrintlnLogger;

public class Starter 
{
	public static SystemOutPrintlnLogger Logger = new SystemOutPrintlnLogger("Starter.class");;
	
	public static void main(String[] args)
	{
		//		TaskAssistant.initializer();
		//		
		//		boolean run = true;
		//		int option = 0;
		//		
		//		while(run)
		//		{
		//			menu();
		//			option = input();
		//			if(option == 2)
		//				run = false;
		//			else if(!TaskAssistant.CONNECTED && option == 1)
		//				TaskAssistant.CONNECTED = TaskAssistant.startChord();
		//			else if(TaskAssistant.CONNECTED && option == 1)
		//				TaskAssistant.sendMessage();
		//		}
		//		TaskAssistant.ocExecuted("exit");
		//		TaskAssistant.suicider();

		Logger.info("Starting Chord4Jtr");
		Starter gui = new Starter();
		gui.connecter();
		
	}

	private void connecter() 
	{		
//		River.connectFrame = new JFrame("Chord Chat");
//		River.connectFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

//		JPanel mainPanel = new JPanel();

//		River.display = new JTextArea(26, 50);
//		River.display.setLineWrap(true);
//		River.display.setWrapStyleWord(true);
//		River.display.setEditable(false);

//		JScrollPane qScroller = new JScrollPane(River.display);
//		qScroller.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
//		qScroller.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
		
//		JButton connectButton = new JButton("Connect to Chord Network");
//		connectButton.addActionListener(new ConnectButtonListener());

//		mainPanel.add(qScroller);
//		mainPanel.add(connectButton);

//		River.connectFrame.getContentPane().add(BorderLayout.CENTER, mainPanel);
//		River.connectFrame.setSize(650, 500);
//		River.connectFrame.setVisible(true);

		River.initializer();

		///////////////////////////////////////////////////////////////////
		River.connectionRenewer();
	}

	private void sender()
	{
		River.dialogFrame = new JFrame("Chord Chat @" + River.BOOTSTRAP);
		River.dialogFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

		JPanel mainPanel = new JPanel();

		River.display = new JTextArea(26, 50);
		River.display.setLineWrap(true);
		River.display.setWrapStyleWord(true);
		River.display.setEditable(false);

		JScrollPane qScroller = new JScrollPane(River.display);
		qScroller.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
		qScroller.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);

		River.input = new JTextField(30);
		JButton sendButton = new JButton("Send Hash");
		sendButton.addActionListener(new SendButtonListener());

		mainPanel.add(qScroller);
		mainPanel.add(River.input);
		mainPanel.add(sendButton);

		River.dialogFrame.getContentPane().add(BorderLayout.CENTER, mainPanel);
		River.dialogFrame.setSize(650, 500);
		River.dialogFrame.setVisible(true);

	}

	public class ConnectButtonListener implements ActionListener
	{
		@Override
		public void actionPerformed(ActionEvent ev) 
		{
			River.CONNECTED = River.startChord();
			if(River.CONNECTED)
			{
				River.connectFrame.dispose();
				sender();
			}
			else
			{			
//				River.display.append("Error while connecting to the network\n");
//				River.display.append("Please try again\n");
			}
		}
	}

	public class SendButtonListener implements ActionListener
	{
		@Override
		public void actionPerformed(ActionEvent ev) 
		{	
			//			if(TaskAssistant.LOCAL_IP.equalsIgnoreCase(TaskAssistant.BOOTSTRAP))
			//	        {
			//	        	TaskAssistant.runner();	
			//	        }
			//			sendButton.
			//			String in = TaskAssistant.input.getText();
			//			if(in.equalsIgnoreCase("file") || in.length()<1)
			//				TaskAssistant.sendMessage();
			//			else
			//				TaskAssistant.sendMessage(in);

			//			TaskAssistant.display.append(TaskAssistant.input.getText() + "\n");
			River.input.setText("");
			River.input.requestFocus();
		}
	}
}
